package Tian7
{
	import Tian7.event.UIEvent;
	
	import flash.display.Sprite;

	/**
	 * 基础 UI 
	 * @author Kakera
	 * 
	 */
	public class UI extends Sprite
	{

		public static const IDLE:String = "idle";
		public static const INIT:String = "init";
		private var _state:State;
		private var _oldState:State;
		private var _data:*;		
		public function UI()
		{
			super();
			state = INIT;
		}
		
		public override function get x ():Number {
			return super.x;
		}
		public override function set x (value:Number):void {
			super.x = value;
		}
		
		
		public override function get y ():Number {
			return super.y;
		}
		public override function set y (value:Number):void {
			super.y = value;
		}
		
		private var _width:int;
		public override function get width():Number {
			return _width;
		}
		public override function set width(value:Number):void {
			setSize(value, _height);
		}
		
		
		private var _height:int;
		public override function get height():Number {
			return _height;
		}
		public override function set height(value:Number):void {
			setSize(_width, value);
		}
		
		
		public function setSize (width:int, height:int):void {
			_width = width;
			_height = height;
			update(width, height);
		} 
		
		public function update (width:int, height:int, data:* = null):void {
			this.data = data;
			dispatchEvent( new UIEvent(UIEvent.UPDATE) );
		}
		
		public function get state ():State {
			return _state;
		}
		public function set state (value:*):void {
			var newState:State = value is String ? new State(value) : value;
			
			// 不允许有同名状态			
			if (!_state || _state.name != newState.name) {
				
				_oldState = _state;
				_state = newState;
				var e:UIEvent = new UIEvent(UIEvent.STATE_CHANGE);
				e.data = {
					oldState : state,
					newState : newState
				}
				dispatchEvent( e );
				
				// 检查与当前 state 如果有同名函数并调用之
				if (this.hasOwnProperty(newState) && this[newState] is Function) {
					this[newState]();
				}
				
			}
		}
		
		public function get oldState ():State {
			return _oldState;
		}
		
		public function get data ():* {
			return _data;
		}
		public function set data (value:*):void {
			if (_data != value) {
				var e:UIEvent = new UIEvent(UIEvent.STATE_CHANGE);
				e.data = {
					oldData : data,
					newData : value
				}
	
				_data = value;
				dispatchEvent( e );
				if (this.hasOwnProperty("renderData") && this["renderData"] is Function) {
					this["renderData"]();
				}
			}
		}
		
		public function get app ():App {
			return App.instance;
		}
		
		public function widthTo (value:int, duration:String, type:String, delay:Number, callback:Function):void {
			
		}
		public function heightTo (value:int, duration:String, type:String, delay:Number, callback:Function):void {
			
		}
		public function xSlideTo (width:int, duration:String, type:String, delay:Number, callback:Function):void {
			
		}
		public function ySlideTo (width:int, duration:String, type:String, delay:Number, callback:Function):void {
			
		}
	}
}